$(function () {
    var getParams = getArgs();
    var currentHotel = getParams.hotel;
    var originalHref = $('.coupon-history').attr('href');
    var couponNo = getParams.no;
    $('.coupon-history').attr('href', originalHref + '?hotel=' + currentHotel);
    toastr.options = {
        "newestOnTop": false,
        "timeOut": 1500,
        "extendedTimeOut": 0,
        "escapeHtml": true,
        "positionClass": "toast-bottom-center"
    };
    if (currentHotel === undefined || currentHotel === '') {
        toastr.error(lang('toastr.grogshop'));
        return;
    }     
    window.checkInDate = new Date().customFormat("yyyy-MM-dd");
    window.checkOutDate = new Date().customDays(1).customFormat("yyyy-MM-dd");
    var docBody = $('#doc-body');
    var couponType = ['',lang('Coupon.01'),lang('Coupon.02')];
    var couponDetail = $('#coupon-detail');
    var weekdayArr = [lang('weekComplete.7'),lang('weekComplete.1'),lang('weekComplete.2'),lang('weekComplete.3'),lang('weekComplete.4'),lang('weekComplete.5'),lang('weekComplete.6')];
    var requestCoupon = function () {
        toggleLoading(true);
        ajaxRequest('/Home/Hotel/couponDetail', {
            "hotel": currentHotel,
            "couponNo": couponNo
        }, 'post', function (data) {
            toggleLoading(false);
            var info = data.data;
            if(info.length !== 0){
                var couponWeek = info.couponWeek;
                var couponDate = info.couponDate;
                couponDetail.find('.coupon-name .info').text(info.couponName);
                couponDetail.find('.coupon-type').text(couponType[info.couponType]);
                couponDetail.find('.coupon-desc').text(lang('Coupon.03')+info.couponPrice+lang('Coupon.04'));
                couponDetail.find('.coupon-data').text(info.couponStart+'-'+info.couponEnd);
                couponDetail.find('.hotel-name').text(info.hotelName);
                if(info.couponType == 1){
                    couponDetail.find('.coupon-price').removeClass('hidden').find('span').text(info.couponMinusPrice);
                }else{
                    couponDetail.find('.coupon-discount').removeClass('hidden').find('span').text(info.couponDiscount);
                }
                var weekDay = '';
                
                $.each(couponWeek,function(item,value){
                    weekDay+= '、'+weekdayArr[value];
                });
                var dateDay = '';
                $.each(couponDate,function(item,value){
                    dateDay+= '、'+value;
                });
                if(weekDay.substring(1)!=''){
                    weekDay =  weekDay.substring(1);
                    if(dateDay.substring(1)!=''){
                        dateDay = '、' + dateDay.substring(1);
                    }            
                }else{
                    if(dateDay.substring(1)!=''){
                        dateDay =  dateDay.substring(1);
                    }            
                }           
                couponDetail.find('.cannot-use').text(weekDay+dateDay)
                if(couponType.length == 0 && couponDate.length == 0){
                    couponDetail.find('.cannot-use').closest('p').addClass('hidden');
                }else{
                    couponDetail.find('.cannot-use').closest('p').removeClass('hidden');
                }
                //房型
                var roomTypeHTML = '';
                var isBookingRoom = parseInt(info.bookmode) === 0;
                var noBookingRoom = parseInt(info.bookmode) === 1;
                $.each(info.roomTypeInfos, function(key, type) {
                    var templateRoomTypeHTML = $($('#templateRoomType').clone().html());
                    templateRoomTypeHTML.find('.room-panel-type-img').attr('data-original', type.coverImageUrl);
                    templateRoomTypeHTML.find('.room-panel-type-name').html(type.name);
                    templateRoomTypeHTML.find('.room-panel-type-price').html(parseFloat(type.price).toFixed(2));
                    var restRoomCount = parseInt(type.rest);
                    if (restRoomCount === 0) {
                        templateRoomTypeHTML.find('.room-panel-type-book').addClass('hidden');
                        if(noBookingRoom){
                            templateRoomTypeHTML.find('.room-panel-type-surplus-info').html(lang('fullHouse'));
                        }
                        if(isBookingRoom){
                            templateRoomTypeHTML.find('.room-panel-type-surplus-info').html(lang('fullHouse'));
                        }
                    } else {
                        templateRoomTypeHTML.find('.room-panel-type-book').removeClass('hidden').attr('data-value', type.id);
                        if (isBookingRoom) {
                            templateRoomTypeHTML.find('.room-panel-type-surplus-info').html(lang('left')+'<span class="room-panel-type-surplus">'+restRoomCount+'</span>'+lang('rooms'));
                        }  
                        if(noBookingRoom){
                            templateRoomTypeHTML.find('.room-panel-type-surplus-info').html('');
                        }
                    }           
                    roomTypeHTML += templateRoomTypeHTML.prop('outerHTML');
                });
                $('#doc .room-panel-types').html(roomTypeHTML);
                docBody.removeClass('hidden');
                $('#doc .room-panel-type-img').lazyload({
                    'skip_invisible': true
                });  
            }else{
                toastr.error(lang('Coupon.06'));
                docBody.addClass('hidden');
            }
        }, function(error){
            toggleLoading(false); 
            toastr.error(typeof error.responseJSON === 'undefined' ? lang('tip.server') : error.responseJSON.info);
        });        
    }
    requestCoupon();
    $('#doc').on('click', '.room-panel-type-book', function(e) {
        e.preventDefault();
        e.stopPropagation();
        var typeId = $(this).data('value');
        window.location.href = '/Home/BookPage/room?hotel=' + currentHotel + '&roomType=' + typeId + '&startDate=' + window.checkInDate + '&endDate=' + window.checkOutDate;
    });    
});
